package Thread;
//import java.util.concurrent.ArrayBlockingDeque;
import java.util.concurrent.BlockingDeque;

class MyThreadPool {
    private BlockingDeque<Runnable> queue = null;

    public MyThreadPool(int n) {
        // /替换为ArrayBlockingDeque
       //queue = new ArrayBlockingDeque<>(1000);
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {
                try {
                    while (true) {
                        Runnable task = queue.take();
                        task.run();
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt(); // 正确处理中断
                }
            });
            thread.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}

public class Demo35_1 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool threadPool = new MyThreadPool(5); // 建议减少线程数（1000太多）
        for (int i = 0; i < 100; i++) {
            int id = i;
            threadPool.submit(() -> {
                System.out.println("[" + Thread.currentThread().getName() + "] id=" + id);
            });
        }
    }
}
